# _*_ coding: UTF-8 _*_
# @Time     : 2021/10/28 13:54
# @Author   : 郑鲁明
# @Site     : http://www.hzdledu.cn/
# @File     : 异常.py
# @Software : PyCharm

# 异常
# 异常是难以预知，在程序中可能出现的错误，中断程序的运行

def foo1():
    for i in range(-2, 3):
        print(20 / i)


# try-except
# 基本语法格式
# try:
#     可能出现异常的代码块
# except [异常类型 [as 别名]]:
#     出现异常之后的处理代码块

# 异常处理：try - except
def foo2():
    for i in range(-2, 3):
        try:
            print(20 / i)
        except Exception as e:
            print(e)


# try-except-else
def foo3():
    for i in range(-2, 3):
        try:
            print(20 / i)
        except Exception as e:
            print(e)
        else:
            print('没有异常')
        print(f'第{i}次循环')


# try-except-else-finally
def foo4():
    for i in range(-2, 3):
        try:
            print(20 / i)
        except Exception as e:
            print(e)
        else:
            print('没有异常')
        finally:
            print('不管有没有异常都要执行')
        print(f'第{i}次循环')


def div(a, b):
    if b == 0:
        raise ZeroDivisionError('除数为0')
    return a / b


def foo5():
    a = int(input('输一个数'))
    b = int(input('再输一个数'))
    try:
        print(div(a,b))
    except Exception as e:
        print(e)

if __name__ == '__main__':
    # foo1()
    # foo2()
    # foo3()
    # foo4()
    foo5()